package com.kc.business.service.impl;

import com.kc.business.domain.BizGoodsBuybackOrder;
import com.kc.business.domain.BizOrderAddress;
import com.kc.business.mapper.BizGoodsBuybackOrderMapper;
import com.kc.business.mapper.BizOrderAddressMapper;
import com.kc.business.service.IBizGoodsBuybackOrderService;
import com.kc.common.exception.CustomException;
import com.kc.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/10/13
 */
@Service
public class BizGoodsBuybackOrderServiceImpl implements IBizGoodsBuybackOrderService {
    @Resource
    BizGoodsBuybackOrderMapper buybackOrderMapper;
    @Resource
    BizOrderAddressMapper orderAddressMapper;

    /**
     * 列表查询方法
     *
     * @param buybackOrder
     * @return
     */
    @Override
    public List<BizGoodsBuybackOrder> getList(BizGoodsBuybackOrder buybackOrder) {
        return buybackOrderMapper.select(buybackOrder);
    }

    /**
     * 修改订单
     *
     * @param buybackOrder
     * @return
     */
    @Override
    public int edit(BizGoodsBuybackOrder buybackOrder) {
        if ("1".equals(buybackOrder.getStatus())) {
            if (buybackOrder.getAddressId() == null) {
                throw new CustomException("地址id不能为空");
            }
            BizOrderAddress orderAddress = orderAddressMapper.selectByPrimaryKey(buybackOrder.getAddressId());
            if (orderAddress == null) {
                throw new CustomException("无效的地址");
            }
            buybackOrder.setAddressee(orderAddress.getName());
            buybackOrder.setBackMobile(orderAddress.getMobile());
            buybackOrder.setBackAddress(orderAddress.getAddress());

        } else if ("2".equals(buybackOrder.getStatus())) {
            if (StringUtils.isBlank(buybackOrder.getRefuseInfo())) {
                throw new CustomException("拒绝原因不能为空");
            }
        } else {
            throw new CustomException("无效的status");
        }
        return buybackOrderMapper.updateByPrimaryKeySelective(buybackOrder);
    }

    /**
     * 确认收货
     *
     * @param buybackOrder
     * @return
     */
    @Override
    public int confirmReceipt(BizGoodsBuybackOrder buybackOrder) {
        return buybackOrderMapper.updateByPrimaryKeySelective(buybackOrder);
    }
}
